package org.example;

import java.io.*;
import java.net.URL;
import java.util.concurrent.TimeUnit;

public class main {
    public static void main(String[] args) {
        try {

            //假定当前
            String PATH = System.getenv("PATH");
            System.out.println("main中获取到的PATH: " + PATH);

            String javaPath = javaCommandPath.getJavaPath();
            // 定义命令
            String command = javaPath + " -Dname=scy -Dage=22 -Dfile.encoding=UTF-8 -Xmx400m ";

            String executeClazz = "org.util.RunJar";

            String arg1 = "/abc/a1";
            String arg2 = "/abc/a2";

            String classPath = System.getProperty("java.class.path");

            String finalCommand = command + " -classpath "+classPath+" " +executeClazz +" "+ arg1 + " " + arg2;

            //home/scy/soft/jdk/jdk1.8.0_66/bin/java -Dname=scy -Dage=22 -Xmx400m
            // -classpath /root/java/myjava/classes:/root/java/myjava/dog.jar  org.util.RunJar /abc/a1 /abc/a2
            System.out.println("执行命令: " + finalCommand);

            URL root = main.class.getClassLoader().getResource("");
            String rootPath = root.getPath();

            // 设置环境变量
            String[] envp = {"ENV1=hello1", "ENV2=hello2", "ENV3=hello3"};



            //从父类继承环境变量
            Process process = Runtime.getRuntime().exec(finalCommand, null, new File(rootPath));

            //自定义环境变量
//      Process process = Runtime.getRuntime().exec(finalCommand, envp, new File(rootPath));

            new Thread(new Runnable(){
                @Override
                public void run() {
                    // 读取正常输出
                    try {
                        InputStream inputStream = process.getInputStream();
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

                        String line;
                        while ((line = bufferedReader.readLine()) != null) {
                            System.out.println("正常输出:"+line);
                        }
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }).start();

            new Thread(new Runnable(){
                @Override
                public void run() {
                    // 读取异常输出
                    try {
                        InputStream inputStream = process.getErrorStream();
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

                        String line;
                        while ((line = bufferedReader.readLine()) != null) {
                            System.out.println("异常输出:"+line);
                        }
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }).start();

            int i = process.waitFor();
            System.out.println("main类等待结束了");
            TimeUnit.SECONDS.sleep(70);
            System.out.println("main类结束了");
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
